package ahblue.cn.tanzi.ui.home.video;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.adapter.BRAdapter;
import ahblue.cn.tanzi.adapter.RvHolder;
import ahblue.cn.tanzi.model.home.VideoCommentlModel;
import ahblue.cn.tanzi.model.home.VideoDetailModel;
import ahblue.cn.tanzi.model.home.VideoModel;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.ui.person.LoginActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import ahblue.cn.tanzi.util.TimeUtils;
import ahblue.cn.tanzi.util.VipUtils;
import ahblue.cn.tanzi.view.CircleImageView;
import ahblue.cn.tanzi.view.video.MediaPlayer;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author pink-jackstraw
 * @date 2018/11/10
 * @describe 视频详情
 */
public class VideoDetailActivity extends BaseActivity implements TextView.OnEditorActionListener{

    @BindView(R.id.recy)
    RecyclerView recyview;
    @BindView(R.id.ic_player)
    MediaPlayer player;
    @BindView(R.id.desc)
    TextView desc;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.mtime)
    TextView mtime;
    @BindView(R.id.tvnickname)
    TextView tvnickname;
    @BindView(R.id.icon)
    CircleImageView icon;
    @BindView(R.id.rlempty)
    RelativeLayout rlempty;
    @BindView(R.id.ivcollect)
    ImageView ivcollect;
    @BindView(R.id.ivprise)
    ImageView ivprise;
    @BindView(R.id.edit_content)
    EditText edit_content;

    public VideoDetailModel mModel;
    private List<VideoCommentlModel.CommentBean> dataList = new ArrayList<>();
    private BRAdapter<VideoCommentlModel.CommentBean> adapter;
    private String id;
    private int page = 0;
    private String pageCount = "100";

    public static void into(Activity activity, String id) {
        Intent intent = new Intent(activity, VideoDetailActivity.class);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);
        ButterKnife.bind(this);
        if (!VipUtils.checkVip(this)) {
            finish();
            return;
        }
        //setbarColor();
        id = getIntent().getStringExtra("id");
        edit_content.setOnEditorActionListener(this);
        edit_content.setOnKeyListener(onKey);
        initView();
        initData();
    }

    private View.OnKeyListener onKey = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                if (!LoginResultModel.checkUserInfoExist(VideoDetailActivity.this)){
                    LoginActivity.into(VideoDetailActivity.this);
                }else {
                    CodeModel.CommentVideo(VideoDetailActivity.this,
                            "3",mModel.id,edit_content.getText().toString(), LoginResultModel.getToken(VideoDetailActivity.this),
                            new CodeModel.SuccessListener() {
                                @Override
                                public void conver(boolean b) {
                                    edit_content.setText("");
                                    VideoCommentlModel.getComment(VideoDetailActivity.this, id, page, pageCount, new VideoCommentlModel.VideoCommentResult() {
                                        @Override
                                        public void conver(VideoCommentlModel model) {
                                            dataList.clear();
                                            dataList.addAll(model.comment);
                                            if (dataList.size() == 0) {
                                                rlempty.setVisibility(View.VISIBLE);
                                                recyview.setVisibility(View.GONE);
                                            } else {
                                                rlempty.setVisibility(View.GONE);
                                                recyview.setVisibility(View.VISIBLE);
                                            }
                                            adapter.notifyDataSetChanged();
                                        }
                                    });
                                }
                            });
                }

            }
            return false;
        }
    };



    private void initData() {
        VideoDetailModel.getDetail(this, id, new VideoDetailModel.VideoDetailResult() {

            @Override
            public void conver(VideoDetailModel model) {
                mModel=model;
                if (null == model) {
                    finish();
                }
                player.setVideoPath(model.file, VideoDetailActivity.this);
                desc.setText(model.describe);
                title.setText(model.title);
                tvnickname.setText(model.title);
                tvnickname.setText(model.user_data.member_name);
                mtime.setText(model.add_time);
                if (model.is_collection ==1){
                    ivcollect.setBackgroundDrawable(getResources().getDrawable(R.drawable.share_icon_collection_p));
                }else {
                    ivcollect.setBackgroundDrawable(getResources().getDrawable(R.drawable.share_icon_collection));
                }

                if (model.is_fabulous ==1){
                    ivprise.setBackgroundDrawable(getResources().getDrawable(R.drawable.rc_praise_hover));
                }else {
                    ivprise.setBackgroundDrawable(getResources().getDrawable(R.drawable.rc_praise));
                }
                ImageLoadUtil.load(VideoDetailActivity.this, model.user_data.member_avatar, icon);
            }
        });

        VideoCommentlModel.getComment(this, id, page, pageCount, new VideoCommentlModel.VideoCommentResult() {
            @Override
            public void conver(VideoCommentlModel model) {
                dataList.addAll(model.comment);
                if (dataList.size() == 0) {
                    rlempty.setVisibility(View.VISIBLE);
                    recyview.setVisibility(View.GONE);
                } else {
                    rlempty.setVisibility(View.GONE);
                    recyview.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initView() {
        recyview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BRAdapter<VideoCommentlModel.CommentBean>(this, R.layout.activity_comment_item, dataList) {
            @Override
            protected void convert(RvHolder holder, VideoCommentlModel.CommentBean specalModel, int position) {
                holder.setText(R.id.name, specalModel.user_data.member_name);
                holder.setText(R.id.content, specalModel.comment_content);
                ImageLoadUtil.load(VideoDetailActivity.this, specalModel.user_data.member_avatar, holder.getView(R.id.img));
                holder.setText(R.id.time, TimeUtils.getStandardDate(specalModel.add_time));
//                holder.setText(R.id.count, "点赞 "+specalModel.fabulous_count + "");
            }
        };
        recyview.setAdapter(adapter);
    }

    //设置沉浸式
    @TargetApi(19)
    void setbarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().setStatusBarColor(this.getResources().getColor(R.color.transparency));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @OnClick({R.id.img_share, R.id.btn_back, R.id.llcollect, R.id.llprise})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_share:
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.llcollect:
                VideoModel.collect(this, "collection", id, LoginResultModel.getToken(this), new CodeModel.SuccessListener() {
                    @Override
                    public void conver(boolean b) {
                        if (b) {
                            ivcollect.setBackgroundDrawable(getResources().getDrawable(R.drawable.share_icon_collection_p));
                        }
                    }
                });
                break;
            case R.id.llprise:
                VideoModel.collect(this, "fabulous", id, LoginResultModel.getToken(this), new CodeModel.SuccessListener() {
                    @Override
                    public void conver(boolean b) {
                        if (b) {
                            ivprise.setBackgroundDrawable(getResources().getDrawable(R.drawable.rc_praise_hover));
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        player.pausePlay();
    }

    @Override
    protected void onDestroy() {
        player.release();
        super.onDestroy();
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        switch (actionId) {
            case EditorInfo.IME_ACTION_UNSPECIFIED:
                Log.e("BALLACK", "IME_ACTION_UNSPECIFIED");
                break;
            default:
                break;
        }

        return true;
    }
}
